<%--
  Created by IntelliJ IDEA.
  User: wyp
  Date: 2020/5/16
  Time: 14:35
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <!--引入JQuery，必须要在bootstrap.js之前引入-->
    <script type="text/javascript" src="./resources/js/jquery-3.4.1.min.js"></script>
    <link rel="stylesheet" href="./resources/css/font.css">
    <link rel="stylesheet" href="./resources/css/xadmin.css">
    <!-- <link rel="stylesheet" href="./css/theme5.css"> -->
    <script src="./resources/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./resources/js/xadmin.js"></script>
    <title>Title</title>
</head>
<script>
    layui.use(['form','layer','jquery'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            form.on('submit(spotadd)',
                function(data) {
                    console.log(data);
                    //发异步，把数据提交给servlet
                    var spotName = $('#spotName').val();
                    var spotPlace = $('#spotPlace').val();
                    var spotItem = $('#spotItem').val();
                    var spotPrice = $('#spotPrice').val();
                    var spotOpenTime = $('#spotOpenTime').val();
                    var spotImgUrl = $('#spotImgUrl').val();

                    $.ajax({
                        type:"POST",
                        data:{"method":"spotAdd","spotName":spotName,"spotPlace":spotPlace,"spotItem":spotItem,"spotPrice":spotPrice,"spotOpenTime":spotOpenTime,"spotImgUrl":spotImgUrl},
                        url:"./spotController",
                        dataType:"json",
                        success:function (data) {
                            layer.alert("增加成功",{icon:6},function () {
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        },
                        error:function (data) {
                            layer.alert("增加失败",{icon:6},function () {
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            });
                        }
                    });
                    return false;
                });


    });
</script>
<body>
    <div class="layui-fluid" >
        <div class="layui-row">
            <form class="layui-form" method="post" >
                <div class="layui-form-item">
                    <label for="spotName" class="layui-form-label">
                        <span class="x-red">*</span>景点名称
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotName" name="spotName" required="required" lay-verify="spotName" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="spotPlace" class="layui-form-label">
                        <span class="x-red">*</span>景点地址
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotPlace" name="spotPlace" required="" lay-verify="spotPlace" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="spotItem" class="layui-form-label">
                        <span class="x-red">*</span>景点项目
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotItem" name="spotItem" required="" lay-verify="spotItem" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="spotPrice" class="layui-form-label">
                        <span class="x-red">*</span>景点价格
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotPrice" name="spotPrice" required="" lay-verify="spotPrice" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="spotOpenTime" class="layui-form-label">
                        <span class="x-red">*</span>开放时间
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="spotOpenTime" name="spotOpenTime" required="" lay-verify="spotOpenTime" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <script type="text/javascript">
                    //图片上传
                    layui.use('upload', function(){
                        var $ = layui.jquery
                            ,upload = layui.upload;
                        //普通图片上传
                        var uploadInst = upload.render({
                            elem: '#test1'
                            ,method:'POST'
                            ,accept:'images'
                            ,url: './uploadImages'
                            ,before: function(obj){
                                //预读本地文件示例，不支持ie8
                                obj.preview(function(index, file, result){
                                    $('#previewImg').attr('src', result); //图片链接（base64）
                                });
                            }
                            ,done: function(res){
                                //上传失败
                                if(res.code > 0){
                                    return layer.msg('上传失败');
                                }
                                //上传成功，返回的路径：res.filePath
                                console.log("文件上传路径："+res.filePath);
                                document.getElementById("spotImgUrl").value = res.filePath;

                            }
                            ,error: function(){
                                //上传失败
                                return layer.msg('上传失败，请重试!');
                            }
                        });
                    });

                </script>
                <%--可做成文件上传形式--%>
                <div class="layui-form-item">
                    <label for="spotImgUrl" class="layui-form-label">
                        <span class="x-red">*</span>景点照片
                    </label>
                    <div class="layui-upload">
                        <button type="button" class="layui-btn" id="test1">上传图片</button>
                        <div class="layui-upload-list ">
                            <img class="layui-upload-img float-right" id="previewImg" style="width: 100px;height: 50px;">
                            <input type="text" id="spotImgUrl" style="display: none" name="spotImgUrl" required="" lay-verify="spotImgUrl" autocomplete="off" class="layui-input">
                        </div>
                    </div>
<%--                    <div class="layui-input-inline">--%>
<%--                        <input type="text" id="spotImgUrl" name="spotImgUrl" required="" lay-verify="spotImgUrl" autocomplete="off" class="layui-input">--%>
<%--                    </div>--%>
                </div>

                <div class="layui-form-item">
                    <button class="layui-btn" lay-filter="spotadd" lay-submit="">增加</button>
                </div>
            </form>
        </div>
    </div>
</body>
</html>
